package com.xiaodou.mjf.application.basic.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.xiaodou.mjf.application.basic.repository.MenuRepository;
import com.xiaodou.mjf.application.basic.repository.RoleRepository;
import com.xiaodou.mjf.application.basic.service.RoleService;
import com.xiaodou.mjf.application.basic.utils.OperatorUtils;
import com.xiaodou.mjf.entity.basic.Menu;
import com.xiaodou.mjf.entity.basic.Role;
import com.xiaodou.mjf.infrastructure.common.app.JpaSimpleService;


/***
 *
 * Role业务访问实现
 * @author XiaoDou.com Code Generator
 * @version 1.0
 * 2014-10-29 17:37:52
 *
 */
@Service @Transactional
public class RoleServiceImpl extends JpaSimpleService<Role,String> implements RoleService{

	Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);

    @Resource
    RoleRepository roleRepository;
    @Resource
    MenuRepository menuRepository;

    @Override
    protected JpaRepository<Role, String> getRepository() {
        return roleRepository;
    }

    public List<Role> list() {
        return roleRepository.list();
    };

    @Override
    public Role save(Role entity) {

        List<Menu> menus = Lists.newArrayList();

        for(Menu menu:entity.getMenus()){
            menu = menuRepository.findOne(menu.getId());
            menus.add(menu);
        }

        entity.setMenus(menus);

        roleRepository.save(entity);

        OperatorUtils.removeCache(OperatorUtils.CACHE_ROLE_LIST);

        return entity;
    }
}

